//
//  DPQingxiSettingView.m
//  DPEduProject
//
//  Created by 边广生 on 2019/3/12.
//  Copyright © 2019 bianguangsheng. All rights reserved.
//

#import "DPQingxiSettingView.h"
#import "UIEnlargeButton.h"
#import "UIView+Additions.h"
#import "DPCommonTool.h"

//屏幕宽高
#define SCREEN_WIDTH ([UIScreen  mainScreen].bounds.size.width)
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define HexColor(rgbValue) UIColorFromRGBA(rgbValue, 1)
#define UIColorFromRGBA(rgbValue, alphaValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0x0000FF))/255.0 \
alpha:alphaValue]

#define settingAnimateDuration  (0.2)

@interface DPQingxiSettingView()
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger baseTAG;
@property (nonatomic,copy) NSString *curTitle;

@end
@implementation DPQingxiSettingView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame settingType:(DPSettinType)settingType {
    if (self = [super initWithFrame:frame]) {
        //codehere....
        if (settingType ==DPSettingQingxidu||settingType == DPSettingSudu) {
            CGFloat vWidth =CGRectGetWidth(frame);
            NSArray *array = nil;
            CGFloat bh = 15;
            CGFloat bw = 30;
            CGFloat vspace = 32;
            NSInteger baseTag = 7000;
            if (settingType ==DPSettingQingxidu) {
                array = @[@"省流",@"流畅",@"高清",@"超清"];
                self.curTitle = @"高清";
            }
            if (settingType == DPSettingSudu) {
                array = @[@"0.5X",@"1X",@"1.2X",@"1.5X",@"2X"];
                bw = 32;
                if (SCREEN_WIDTH>=375) {
                    vspace = 28;
                }else{
                vspace = (28/375)*SCREEN_WIDTH+5;
                }
                baseTag = 8000;
                self.curTitle = @"1X";
            }
            
            self.baseTAG = baseTag;
            
            CGFloat topMargin =(SCREEN_WIDTH- ((bh*array.count)+((array.count-1)*vspace)))/2;
            if (SCREEN_WIDTH <375 &&settingType == DPSettingSudu) {
                topMargin = (SCREEN_WIDTH- ((bh*array.count)+((array.count-1)*28)))/2;
            }
            self.dataArray = [NSMutableArray arrayWithArray:array];
            for (int i = 0; i<array.count; i++) {
                NSString *title = array[i];
                UIEnlargeButton *btn = [[UIEnlargeButton alloc]initWithFrame:CGRectMake((vWidth-bw)/2, topMargin+(bw+vspace)*i, bw, bh)];
                [btn setTitle:title forState:UIControlStateNormal];
                if ([title isEqualToString: self.curTitle]) {
                    [btn setTitleColor:HexColor(0x00B93B) forState:UIControlStateNormal];
                }else{
                    [btn setTitleColor:HexColor(0xFFFFFF) forState:UIControlStateNormal];
                }
                [btn.titleLabel setFont:[UIFont fontWithName:@"PingFangSC-Medium" size: 15]];
                btn.tag = baseTag+i;
                [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:btn];
            }
        }
        if (settingType == DPSettinTypeYinLiang) {
            [self addSubview:self.hVolumeSlider];
            [self addSubview:self.ylLb];
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapQx:)];
        tap.numberOfTapsRequired = 1;
        [self addGestureRecognizer:tap];
        
    }
    return self;
    
}
- (UILabel *)ylLb{
    if (!_ylLb) {
        _ylLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 10)];
        _ylLb.text = @"75%";
        _ylLb.textColor = HexColor(0xFFFFFF);
        _ylLb.font = [UIFont fontWithName:@"PingFangSC-Semibold" size: 10];
        _ylLb.textAlignment = NSTextAlignmentCenter;
    }
    return _ylLb;
    
}
- (UISlider *)hVolumeSlider {
    if (!_hVolumeSlider) {
        _hVolumeSlider = [[UISlider alloc]initWithFrame:CGRectMake(0, 0, 20, SCREEN_HEIGHT-83-40)];
        _hVolumeSlider.backgroundColor = [UIColor clearColor];//test
        _hVolumeSlider.minimumValue = 0;
        _hVolumeSlider.maximumValue = 1;
//        [_hVolumeSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
        _hVolumeSlider.value = 0.3;
        UIImage *stetchLeftTrack= [UIImage imageNamed:@"tab_beijing"];
        
        UIImage *stetchRightTrack = [DPCommonTool imageWithColor:[UIColor whiteColor]];
        
        //滑块图片
        UIImage *thumbImage = [UIImage imageNamed:@"yinliang_yuanquan"];//缺少图
        [_hVolumeSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
        [_hVolumeSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
        [_hVolumeSlider setThumbImage:thumbImage forState:UIControlStateNormal];
    }
    return _hVolumeSlider;
}




- (void)tapQx:(UITapGestureRecognizer *)tap{
//    self.hidden = YES;
    [UIView animateWithDuration:settingAnimateDuration animations:^{
        self.frame  = CGRectMake(SCREEN_WIDTH, self.top, self.width, self.height);
        self.hidden = YES;
    }];
    
    
}
- (void)CloseView{
    [UIView animateWithDuration:settingAnimateDuration animations:^{
        self.frame  = CGRectMake(SCREEN_WIDTH, self.top, self.width, self.height);
        self.hidden = YES;
    }];
    
}
- (void)btnClick:(UIButton *)btn{
    if ([btn.currentTitle isEqualToString:self.curTitle]) {
        NSLog(@"选择当前项目");
    }else{
    for (int i= 0;i<self.dataArray.count;i++) {
         UIButton *abtn = (UIButton *)[self viewWithTag:self.baseTAG+i];
        if (abtn ==btn) {
            //select btn
            [btn setTitleColor:HexColor(0x00B93B) forState:UIControlStateNormal];
        }else{
            
           [abtn setTitleColor:HexColor(0xFFFFFF) forState:UIControlStateNormal];
        }
    }
    if (self.selectQingxiDuBlock) {
        self.selectQingxiDuBlock(btn.currentTitle);
    }
    }
    self.curTitle = btn.currentTitle;
    [UIView animateWithDuration:settingAnimateDuration animations:^{
        self.frame  = CGRectMake(SCREEN_WIDTH, self.top, self.width, self.height);
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
    
    
    
}
- (void)releaseBlock{
    self.selectQingxiDuBlock = nil;
    
}
- (void)dealloc{
    NSLog(@"%@-dealloc",NSStringFromClass(self.class));
    
}
@end
